package cn.iocoder.yudao.module.contract.dal.mysql.invoice;

import java.lang.reflect.Array;
import java.util.*;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.framework.mybatis.core.query.MPJLambdaWrapperX;
import cn.iocoder.yudao.module.contract.controller.admin.invoiceContract.vo.InvoiceContractPageRespVO;
import cn.iocoder.yudao.module.contract.dal.dataobject.client.ClientDO;
import cn.iocoder.yudao.module.contract.dal.dataobject.invoice.InvoiceDO;
import cn.iocoder.yudao.module.contract.dal.dataobject.invoiceContract.InvoiceContractDO;
import cn.iocoder.yudao.module.contract.dal.dataobject.order.OrderDO;
import cn.iocoder.yudao.module.contract.dal.dataobject.orderContract.OrderContractDO;
import cn.iocoder.yudao.module.contract.utils.StringUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.ibatis.annotations.Mapper;
import cn.iocoder.yudao.module.contract.controller.admin.invoice.vo.*;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Update;

/**
 * 发票 Mapper
 *
 * @author 管理员
 */
@Mapper
public interface InvoiceMapper extends BaseMapperX<InvoiceDO> {

    default PageResult<InvoicePageRespVO> selectPage(InvoicePageReqVO reqVO) {
        return selectJoinPage(reqVO, InvoicePageRespVO.class, new MPJLambdaWrapperX<InvoiceDO>()
                .betweenIfPresent(InvoiceDO::getAudat, reqVO.getAudat())
                .betweenIfPresent(InvoiceDO::getKprq, reqVO.getKprq())
                .betweenIfPresent(InvoiceDO::getContractGeneratedTime, reqVO.getContractGeneratedTime())
                .selectAll(InvoiceDO.class) //
                .selectAssociation(ClientDO.class, InvoiceContractPageRespVO::getClientDO) //
                .in("1".equals(reqVO.getListType()),InvoiceDO::getStatus,Arrays.asList("0,2".split(",")))
                .in(!ObjectUtils.isEmpty(reqVO.getXfmcList()),InvoiceDO::getXfmc, Arrays.asList(!ObjectUtils.isEmpty(reqVO.getXfmcList())?reqVO.getXfmcList():new Object[]{""}))
                .like(!ObjectUtils.isEmpty(reqVO.getGfmc()) && "1".equals(reqVO.getGfmcType()),InvoiceDO::getGfmc, reqVO.getGfmc())
                .in(!ObjectUtils.isEmpty(reqVO.getGfmc()) && "2".equals(reqVO.getGfmcType()),InvoiceDO::getGfmc, !ObjectUtils.isEmpty(reqVO.getGfmc()) ?reqVO.getGfmc().split(","): new Object[]{""})
                .in(!ObjectUtils.isEmpty(reqVO.getVsbedList()),InvoiceDO::getVsbed, Arrays.asList(!ObjectUtils.isEmpty(reqVO.getVsbedList())?reqVO.getVsbedList():new Object[]{""}))
                .in(!ObjectUtils.isEmpty(reqVO.getAuartList()),InvoiceDO::getAuart, Arrays.asList(!ObjectUtils.isEmpty(reqVO.getAuartList())?reqVO.getAuartList():new Object[]{""}))

                .like(!ObjectUtils.isEmpty(reqVO.getFphm()) && "1".equals(reqVO.getFphmType()),InvoiceDO::getFphm, reqVO.getFphm())
                .in(!ObjectUtils.isEmpty(reqVO.getFphm()) && "2".equals(reqVO.getFphmType()),InvoiceDO::getFphm, !ObjectUtils.isEmpty(reqVO.getFphm()) ?reqVO.getFphm().split(","): new Object[]{""})

                .eq(!ObjectUtils.isEmpty(reqVO.getInvoiceMonth()),InvoiceDO::getInvoiceMonth,reqVO.getInvoiceMonth())

                .like(!ObjectUtils.isEmpty(reqVO.getDdh()) && "1".equals(reqVO.getDdhType()),InvoiceDO::getDdh, reqVO.getDdh())
                .in(!ObjectUtils.isEmpty(reqVO.getDdh()) && "2".equals(reqVO.getDdhType()),InvoiceDO::getDdh, !ObjectUtils.isEmpty(reqVO.getDdh()) ?reqVO.getDdh().split(","): new Object[]{""})

                .like(!ObjectUtils.isEmpty(reqVO.getQyusername()) && "1".equals(reqVO.getQyusernameType()),InvoiceDO::getQyusername, reqVO.getQyusername())
                .in(!ObjectUtils.isEmpty(reqVO.getQyusername()) && "2".equals(reqVO.getQyusernameType()),InvoiceDO::getQyusername, !ObjectUtils.isEmpty(reqVO.getQyusername()) ?reqVO.getQyusername().split(","): new Object[]{""})

                .like(!ObjectUtils.isEmpty(reqVO.getSalesmethodname()) && "1".equals(reqVO.getSalesmethodnameType()),InvoiceDO::getSalesmethodname, reqVO.getSalesmethodname())
                .in(!ObjectUtils.isEmpty(reqVO.getSalesmethodname()) && "2".equals(reqVO.getSalesmethodnameType()),InvoiceDO::getSalesmethodname, !ObjectUtils.isEmpty(reqVO.getSalesmethodname()) ?reqVO.getSalesmethodname().split(","): new Object[]{""})

                .in(!ObjectUtils.isEmpty(reqVO.getFpztList()),InvoiceDO::getFpzt, Arrays.asList(!ObjectUtils.isEmpty(reqVO.getFpztList())?reqVO.getFpztList():new Object[]{""}))

                .like(!ObjectUtils.isEmpty(reqVO.getContractNo()) && "1".equals(reqVO.getContractNoType()),InvoiceDO::getContractNo, reqVO.getContractNo())
                .in(!ObjectUtils.isEmpty(reqVO.getContractNo()) && "2".equals(reqVO.getContractNoType()),InvoiceDO::getContractNo, !ObjectUtils.isEmpty(reqVO.getContractNo()) ?reqVO.getContractNo().split(","): new Object[]{""})

                .in(!ObjectUtils.isEmpty(reqVO.getStatusList()),InvoiceDO::getStatus, Arrays.asList(!ObjectUtils.isEmpty(reqVO.getStatusList())?reqVO.getStatusList():new Object[]{""}))

                .leftJoin(ClientDO.class, ClientDO::getClientCode, InvoiceContractDO::getClientCode)
                .in(!ObjectUtils.isEmpty(reqVO.getZzhtgklx()),ClientDO::getZzhtgklx,  !ObjectUtils.isEmpty(reqVO.getZzhtgklx()) ?reqVO.getZzhtgklx().split(","): new Object[]{""})

                .orderByDesc(ObjectUtils.isEmpty(reqVO.getOrder()),InvoiceDO::getKprq)
                .orderBy(ObjectUtils.isNotEmpty(reqVO.getOrder()),"ascending".equals(reqVO.getOrder()),getFieldName(reqVO.getProp()))
        );
    }

    default String getFieldName(String prop){
        return  StringUtils.toUnderScoreCase(prop);
    }
    void batchInsertInvoices(@Param("list") List<InvoiceDO> list);

     @Update("UPDATE ct_invoice AS i JOIN ct_invoice AS oi ON i.fphm = oi.yfphm SET i.status = -1 WHERE i.status != 1")
     int updateInvalidInvoice();
}